package com.gz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gz.entity.Book;
import com.gz.entity.MyPageData;
import com.gz.entity.Vo.BookVo;
import com.gz.service.BookService;
import com.gz.service.BookTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @Autowired
    BookTypeService bookTypeService;



    /*
     * 获取所有图书信息
     * */
    @PostMapping("/getBookAll/{pageNum}/{pageSize}")
    public ResponseEntity<?> getBookAll(@PathVariable int pageNum,
                                        @PathVariable int pageSize,
                                        @RequestBody HashMap<String,Object> map) {

        String keyword = map.get("keyword").toString();
        Page<Book> page = bookService.getBookAll(pageNum,pageSize,keyword);
        List<Book> books = page.getRecords();
        List<BookVo> bookTypeInfoVOS = new ArrayList<>();
        books.forEach(book -> {
            BookVo bookTypeInfoVO = new BookVo();
            BeanUtils.copyProperties(book, bookTypeInfoVO);
            bookTypeInfoVOS.add(bookTypeInfoVO);
        });
        MyPageData data = new  MyPageData(page.getTotal(),bookTypeInfoVOS);
        return ResponseEntity.ok(data);
    }



    /*
     * 根据图书类型id获取对应的图书信息
     * */
    @PostMapping("/getBookByTypeId/{typeId}/{pageNum}/{pageSize}")
    public ResponseEntity<?> getBookByCondition(@PathVariable Integer typeId,
                                             @PathVariable int pageNum,
                                             @PathVariable int pageSize
                                            ) {
        Page<Book> page=bookService.getBookTypeById(typeId,pageNum,pageSize);
        List<Book> books = page.getRecords();
        List<BookVo> bookTypeInfoVOS = new ArrayList<>();
        books.forEach(book -> {
            BookVo bookTypeInfoVO = new BookVo();
            BeanUtils.copyProperties(book, bookTypeInfoVO);
            bookTypeInfoVOS.add(bookTypeInfoVO);
        });
        MyPageData data = new  MyPageData(page.getTotal(),bookTypeInfoVOS);
        return ResponseEntity.ok(data);
    }
    @PostMapping("/getBookById/{bookId}")
    public ResponseEntity<?> getBookById(@PathVariable Long bookId) {
        Book book = bookService.getBookById(bookId);
        return ResponseEntity.ok(book);
    }

}
